from bs4 import BeautifulSoup
from requests import get as s_get
from httpx import get as x_get

url = "https://s.weibo.com/top/summary"  # 微博热搜

web_html = s_get(url, timeout=30, verify=False)
# print(web_html.status_code)
# print(web_html.headers)
# print(web_html.content)
# print(web_html.text)
# print(web_html.url)
# print(web_html.encoding)


bs = BeautifulSoup(web_html.content, 'html.parser')
# bs = BeautifulSoup(web_html.content, 'lxml')
# 直接取文本，更简洁，属于字符串形式，看上去比较零散，因为有很多'\n'
# print(bs.get_text())
# 格式化代码，对齐、缩进、换行等
# print(bs.prettify())


with open("test_html.html", "w", encoding="utf-8") as f:
    f.write(bs.prettify())

print(bs.title.string)
print(bs.title)  # <title>Sina Visitor System</title>

timehot_rank_list = []
timehot_href_list = []
timehot_content_list = []
timehot_num_list = []
timehot_div = bs.find("div", {"class": "data", "id": "pl_top_realtimehot"}) # 如果是字典形式，则key要与html文件中的一致
# timehot_tbody_text = bs.find("tbody").get_text()
# print(timehot_tbody_text)

# timehot_tbody = bs.find("tbody")
# print(timehot_tbody)

timehot_tbody = timehot_div.tbody
print(timehot_tbody)

return_str = ""
for index, element in enumerate(timehot_tbody.find_all("tr")):
    rank = element.find("td", class_="td-01 ranktop")
    td = element.find("td", class_="td-02")
    timehot_href_list.append(td.a["href"])
    timehot_content_list.append(td.a.string)

    if index == 0:
        timehot_rank_list.append("0")
        timehot_num_list.append("9999999999")
        return_str = return_str + '\t' + '0' + '\t' + td.a.string + '\t' + td.a['href'] + '\t' + '9999999999' + '\n'
    else:
        timehot_rank_list.append(rank.string)
        timehot_num_list.append(td.span.string)
        return_str = return_str + '\t' + rank.string + '\t' + td.a.string + '\t' + td.a['href'] + '\t' + td.span.string + '\n'

with open("微博热搜榜.txt", "w", encoding="utf-8") as f:
    f.write(return_str)
